package SearchMethods;

public class MidSearch {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 61, 777};
        int i = chaZ(arr, 61);

        System.out.println(i);
        anIntd(arr, 3);

    }

    public static int chaZ(int[] arr, int target) {

        int left = 0;
        int right = arr.length-1;
        int mid;
        while (left < right) {
            System.out.println("二分查找次数");
            mid = left + right >>> 1;
            if (arr[mid] == target) {
                return mid;
            }
            if (arr[mid] < target) {
                left = mid + 1;
            }
            if (arr[mid] > target) {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static int anIntd(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = left + (right - left) / 2;
        while (left < right) {
            if (target == nums[mid]) {
                return mid;
            }
            if (nums[mid] > target) {
                right = mid - 1;
            }
            if (nums[mid] < target) {
                left = mid + 1;
            }
            mid = left + (right - left) / 2;
        }
        return left;
    }
}
